<template>
	<view class="container">
		<block v-if="isload">
			<dd-tab :itemdata="[t('佣金')+'明细','提现记录']" :itemst="['0','1']" :st="st" :isfixed="false"
				@changetab="changetab"></dd-tab>
			<view class="content">
				<block v-if="st==0">
					<view v-for="(item, index) in datalist" :key="index" class="item">
						<view class="f1">
							<text class="t1">{{item.remark}}</text>
							<text class="t2">{{item.createtime}}</text>
							<text class="t3">变更后佣金: {{item.after}}元</text>
						</view>
						<view class="f2">
							<text class="t1" v-if="item.money>0">+{{item.money}}</text>
							<text class="t2" v-else>{{item.money}}</text>
						</view>
					</view>
				</block>
				<block v-if="st==1">
					<view v-for="(item, index) in datalist" :key="index" class="item">
						<view class="f1">
							<text class="t1">提现金额：{{item.money}}元</text>
							<text class="t2">{{item.createtime}}</text>
						</view>
						<view class="f3">
							<text class="t1" v-if="item.status==0">审核中</text>
							<view class="t11" v-if="item.status==1">
								<text class="t1">审核通过</text>
								<!-- <button class="t11btn" @tap="getMoney">领取</button> -->
							</view>
							<text class="t2" v-if="item.status==2">已驳回</text>
							<text class="t1" v-if="item.status==3">已打款</text>
						</view>
					</view>
				</block>
			</view>
		</block>
		<nodata v-if="nodata"></nodata>
		<nomore v-if="nomore"></nomore>
		<loading v-if="loading"></loading>
		<dp-tabbar :opt="opt"></dp-tabbar>
		<popmsg ref="popmsg"></popmsg>
	</view>
</template>

<script>
	var app = getApp();
	export default {
		data() {
			return {
				opt: {},
				loading: false,
				isload: false,
				menuindex: -1,
				nodata: false,
				nomore: false,
				st: 0,
				datalist: [],
				textset: {},
				pagenum: 1,
				wxConfig: {
					appId: '',
					timestamp: '',
					nonceStr: '',
					signature: '',
					jsApiList: ['requestMerchantTransfer']
				},
				mchId: null, //【商户号】商户号，由微信支付生成并下发
				package: null, //【跳转页面的package信息】商家转账付款单跳转收款页package信息,商家转账付款单受理成功时返回给商户
			};
		},

		onLoad: function(opt) {
			this.opt = app.getopts(opt);
			this.st = this.opt.st || 0;
			var that = this;
			this.getdata();
		},
		onPullDownRefresh: function() {
			this.getdata();
		},
		onReachBottom: function() {
			if (!this.nodata && !this.nomore) {
				this.pagenum = this.pagenum + 1;
				this.getdata(true);
			}
		},
		methods: {
			getdata: function(loadmore) {
				if (!loadmore) {
					this.pagenum = 1;
					this.datalist = [];
				}
				var that = this;
				var st = that.st;
				var pagenum = that.pagenum;
				that.loading = true;
				that.nodata = false;
				that.nomore = false;
				app.post('Agent/commissionlog', {
					st: st,
					pagenum: pagenum
				}, function(res) {
					that.loading = false;
					var data = res.data;
					if (pagenum == 1) {
						that.textset = app.globalData.textset;
						uni.setNavigationBarTitle({
							title: that.t('佣金') + '明细'
						});
						that.datalist = data;
						if (data.length == 0) {
							that.nodata = true;
						}
						that.loaded();
					} else {
						if (data.length == 0) {
							that.nomore = true;
						} else {
							var datalist = that.datalist;
							var newdata = datalist.concat(data);
							that.datalist = newdata;
						}
					}
				});
			},
			changetab: function(st) {
				this.st = st;
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.getdata();
			},

			getMoney: function(e) {
				var that = this;
				that.loading = true;
				that.initReq();
				// #ifdef MP-WEIXIN
				if (wx.canIUse('requestMerchantTransfer')) {
					wx.requestMerchantTransfer({
						mchId: that.mchId,
						appId: wx.getAccountInfoSync().miniProgram.appId,
						package: that.package,
						success: (res) => {
							// res.err_msg将在页面展示成功后返回应用时返回ok，并不代表付款成功
							console.log('success:', res);
							app.success(res);
						},
						fail: (res) => {
							console.log('fail:', res);
							app.error(res);
						},
					});
				} else {
					wx.showModal({
						content: '你的微信版本过低，请更新至最新版本。',
						showCancel: false,
					});
				}
				// #endif
				// #ifdef H5
				that.initWxSdkConfig()
				// #endif
				that.loaded();
			},

			initReq() {
				app.get('xxx', {}, function(res) {
					this.mchId = res.data.mchId;
					this.package = res.data.package;
				});
			},

			initWxSdkConfig() {
				// 从服务器获取签名信息
				app.get('xxx', {}, function(res) {
						console.log("res:",res);
					if(res.status == 200){
						this.wxConfig = res.data;
						this.configWxSdk();
					}else{
						app.error(res)
					}

				});
			},

			configWxSdk() {
				wx.config({
					debug: false, // 调试模式
					appId: this.wxConfig.appId,
					timestamp: this.wxConfig.timestamp,
					nonceStr: this.wxConfig.nonceStr,
					signature: this.wxConfig.signature,
					jsApiList: this.wxConfig.jsApiList // 需要使用的 JSAPI 列表
				});

				wx.ready(() => {
					this.checkJsApi();
				});

				wx.error((err) => {
					console.error('微信 JS-SDK 配置失败:', err);
				});
			},

			checkJsApi() {
				wx.checkJsApi({
					jsApiList: ['requestMerchantTransfer'],
					success: (res) => {
						if (res.checkResult['requestMerchantTransfer']) {
							this.requestMerchantTransfer();
						} else {
							uni.showToast({
								title: '你的微信版本过低，请更新至最新版本。',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						console.error('检查 JSAPI 失败:', err);
					}
				});
			},

			requestMerchantTransfer() {
				WeixinJSBridge.invoke(
					'requestMerchantTransfer', {
						mchId: this.mchId,
						appId: 'wxd441dec22ba4fdff',
						package: this.package,
					},
					(res) => {
						if (res.err_msg === 'requestMerchantTransfer:ok') {
							uni.showToast({
								title: '请求成功',
								icon: 'success'
							});
						} else {
							uni.showToast({
								title: '请求失败',
								icon: 'none'
							});
						}
					}
				);
			}
		}
	};
</script>
<style>
	.content {
		width: 94%;
		margin: 20rpx 3%;
	}

	.content .item {
		width: 100%;
		background: #fff;
		margin: 20rpx 0;
		padding: 40rpx 30rpx;
		border-radius: 8px;
		display: flex;
		align-items: center
	}

	.content .item:last-child {
		border: 0
	}

	.content .item .f1 {
		width: auto;
		display: flex;
		flex-direction: column
	}

	.content .item .f1 .t1 {
		color: #000000;
		font-size: 30rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.content .item .f1 .t2 {
		color: #666666;
		font-size: 24rpx;
		margin-top: 10rpx
	}

	.content .item .f1 .t3 {
		color: #666666;
		font-size: 24rpx;
		margin-top: 10rpx
	}

	.content .item .f2 {
		flex: 1;
		width: auto;
		font-size: 36rpx;
		text-align: right
	}

	.content .item .f2 .t1 {
		color: #03bc01
	}

	.content .item .f2 .t2 {
		color: #000000
	}

	.content .item .f3 {
		flex: 1;
		width: auto;
		font-size: 32rpx;
		text-align: right
	}

	.content .item .f3 .t1 {
		color: #03bc01
	}

	.content .item .f3 .t2 {
		color: #000000
	}

	.t11 {
		float: right
	}

	.t11btn {
		display: inline-block;
		background: #BA1F25;
		color: #fff;
		border-radius: 5px;
		margin-left: 10px;
		padding: 0 10px
	}

	.data-empty {
		background: #fff
	}
</style>